gen id = session*100+id_in_session

//average per round
bysort round treatment : egen avoutput_round=mean( output ) if is_bot==0
bysort round treatment : egen avreport_round=mean( report ) if is_bot==0
bysort round treatment : egen avshare_misrep_round=mean( share_misrep ) if is_bot==0

//average per participants
 bysort id : egen avoutput=mean( output )
 bysort id : egen avreport=mean( report )
 
//Bot variables
gen is_bot = .
replace is_bot=1 if label=="OAEP6" & session==1
replace is_bot=1 if label=="OAEP18" & session==2
replace is_bot=1 if label=="OAEP1" & session==2
replace is_bot=1 if label=="OAEP12" & session==3
replace is_bot=0 if is_bot==.

//group that contain one bot
bysort Group round : egen team_bot=total( is_bot )

//individuals who met a bot in the previous round
sort session Group round id
by id: gen met_bot_lag1 = team_bot[_n-1]
replace met_bot_lag1 = 0 if met_bot_lag1 == .

//individuals who met a bot in any previous round
gen nb_met_bot = .
	
forvalues p = 1/7 {
	forvalues g = 101/815 {
		cap {
			su team_bot if team_bot == 1 & id == `g' & round < `p', detail
			replace nb_met_bot = r(N) if id == `g' & round == `p'
			}
		}
	}
	
	
gen met_bot_prev = 1 if nb_met_bot > 0
replace met_bot_prev = 0 if met_bot_prev == .



 //av output group
gen Group = session*1000+round*100+group_id 
gen GA_output = .

forvalues p = 1/7 {
	forvalues g = 1101/8705 {
		cap {
			su output if id_in_group!=1 & is_bot==0 & round == `p' & Group == `g', detail
			replace GA_output = r(mean) if id_in_group==1 & round == `p' & Group == `g'
			}
		}
	}

forvalues p = 1/7 {
	forvalues g = 1101/8705 {
		cap {
			su output if id_in_group!=2 & is_bot==0 & round == `p' & Group == `g', detail
			replace GA_output = r(mean) if id_in_group==2 & round == `p' & Group == `g'
			}
		}
	}
	
	
forvalues p = 1/7 {
	forvalues g = 1101/8705 {
		cap {
			su output if id_in_group!=3 & is_bot==0 & round == `p' & Group == `g', detail
			replace GA_output = r(mean) if id_in_group==3 & round == `p' & Group == `g'
			}
		}
	}
 
 
 
 //av report group

gen GA_report = .

forvalues p = 1/7 {
	forvalues g = 101/805 {
		cap {
			su report if id_in_group!=1 & is_bot==0 & round == `p' & Group == `g', detail
			replace GA_report = r(mean) if id_in_group==1 & round == `p' & Group == `g'
			}
		}
	}

forvalues p = 1/7 {
	forvalues g = 101/805 {
		cap {
			su report if id_in_group!=2 & is_bot==0 & round == `p' & Group == `g', detail
			replace GA_report = r(mean) if id_in_group==2 & round == `p' & Group == `g'
			}
		}
	}
	
	
forvalues p = 1/7 {
	forvalues g = 101/805 {
		cap {
			su report if id_in_group!=3 & is_bot==0 & round == `p' & Group == `g', detail
			replace GA_report = r(mean) if id_in_group==3 & round == `p' & Group == `g'
			}
		}
	}
	
	
//Lagged variables
sort id round 
by id: gen audit_lag1 = audit[_n-1]	
 
 

